package com.ruoyi.web.controller.question;

import com.github.pagehelper.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.domain.question.Question;
import com.ruoyi.common.domain.question.QuestionObject;
import com.ruoyi.common.domain.question.Subject;
import com.ruoyi.common.domain.question.TextContent;
import com.ruoyi.common.domain.question.vo.QuestionEditRequestVM;
import com.ruoyi.common.domain.question.vo.QuestionResponseVM;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.question.ExamUtil;
import com.ruoyi.question.service.IQuestionService;
import com.ruoyi.question.service.ISubjectService;
import com.ruoyi.question.service.ITextContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@RestController()
@RequestMapping(value = "/question")
public class QuestionController extends BaseController {

    @Autowired
    private IQuestionService biz;

    @Autowired
    private ITextContentService textContentService;

    @GetMapping("/list")
    public TableDataInfo list(QuestionEditRequestVM record)
    {
        startPage();
        Page<Question> list = (Page<Question>) biz.selectList(record);
        List<QuestionResponseVM> res = new ArrayList<>();
        list.forEach(q -> {
            QuestionResponseVM vm = modelMapper.map(q, QuestionResponseVM.class);
            vm.setScore(ExamUtil.scoreToVM(q.getScore()));
            vm.setCreateTime(DateUtils.getFormatTime(q.getCreateTime(), DateUtils.DEFAULT_DATE_FORMAT));
            TextContent textContent = textContentService.selectById(q.getInfoTextContentId());
            QuestionObject questionObject = JsonUtil.toJsonObject(textContent.getContent(), QuestionObject.class);
            String clearHtml = StringUtils.clearHtml(questionObject.getTitleContent());
            vm.setShortTitle(clearHtml);
            res.add(vm);
        });
        return getDataTable(res, list.getTotal());
    }


    @GetMapping(value = "/info/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(biz.findById(id));
    }

    @PostMapping(value = "/edit")
    public AjaxResult edit(@RequestBody @Valid QuestionEditRequestVM model) {
        String errMsg = model.checkParams();
        if(StringUtils.isNotEmpty(errMsg)) {
            return AjaxResult.error(errMsg);
        }
        if (null == model.getId()) {
            biz.insertFullQuestion(model, getUsername());
        } else {
            biz.updateFullQuestion(model, getUsername());
        }
        return success();
    }

    @PostMapping(value = "/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        Question question = biz.selectById(id);
        question.setDeleted(true);
        biz.updateByIdFilter(question);
        return success();
    }

}
